home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_C / XBITMAP_ / XBITMAP.C < prev    next >
C/C++ Source or Header  |  1989-03-21  |  18KB  |  767 lines

  1. /*
  2.  * Program to convert X11 Bitmap files to Macintosh 'PICT' files,
  3.  * and vice versa.  Also useful as a viewer.
  4.  *
  5.  * This source code is hereby placed into the public domain.
  6.  *
  7.  * Earle R. Horton  Friday, March 10, 1989
  8.  */
  9. #include <Stdio.h>
  10. #include <Quickdraw.h>
  11. #include <Menus.h>
  12. #include <Windows.h>
  13. #include <Events.h>
  14. #include <Controls.h>
  15. #include <Dialogs.h>
  16. #include <Fonts.h>
  17. #include <Desk.h>
  18. #include <Files.h>
  19. #include <Memory.h>
  20. #include <Packages.h>
  21. #include <Resources.h>
  22. #include <Errors.h>
  23.  
  24. #ifndef TRUE
  25. #define    TRUE    (-1)
  26. #define    FALSE    0
  27. #endif
  28.  
  29. #ifdef macintosh
  30. #include <compat.h>
  31. #endif
  32.  
  33. #define lastMenu 3
  34. #define appleMenu 1
  35. #define fileMenu 256
  36. #define editMenu 257
  37. #define openX 1
  38. #define openPICT 2
  39. #define openPNTG 3
  40. #define saveX 5
  41. #define savePICT 6
  42. #define savePNTG 7
  43. #define Quit 9
  44.  
  45. MenuHandle myMenu[lastMenu];
  46. Rect dragRect, pRect;
  47. EventRecord myEvent;
  48. int refNum, MyControl, doneFlag;
  49. int scale, code;
  50. WindowRecord wRecord;
  51. WindowPtr theWindow, whichWindow;
  52. char *icons[6][48];
  53. ControlHandle hScroll, vScroll, whichControl;
  54. Point theOrigin;
  55. RgnHandle theUpdateRgn;
  56. Handle XCursor;
  57.  
  58. GrafPort srcport;
  59.  
  60. DieGraceFully()
  61. {
  62.     exit();
  63. }
  64. main()
  65. {
  66.     int t;
  67.     GrafPtr savePort;
  68.     GrafPort XPort;
  69.     long WaitUntil;
  70.     pascal void ScrollUp(),ScrollDown();
  71.  
  72.     InitGraf(&qd.thePort);
  73.     InitFonts();
  74.     FlushEvents(everyEvent,0);
  75.     InitWindows();
  76.     XCursor = GetResource('CURS',256);
  77.     InitCursor();
  78.     if(XCursor != nil){
  79.         HLock(XCursor);
  80.         SetCursor(*XCursor);
  81.     }
  82.     else{
  83.         SetCursor(&qd.arrow);
  84.     }
  85.  
  86.     dragRect = qd.screenBits.bounds;
  87.     dragRect.top += 24;
  88.     dragRect.left +=4;
  89.  
  90.     doneFlag = FALSE;
  91.  
  92.     OpenPort(&XPort);
  93.     SetPort(&XPort);
  94.     PenPat(&qd.gray);
  95.     PaintRect(&XPort.portRect);
  96.     WaitUntil = TickCount() + 60L*8L;
  97.     while(WaitUntil > TickCount()){
  98.         if(Button()){
  99.             break;
  100.         }
  101.     }
  102.     ClosePort(&XPort);
  103.     
  104.     theWindow = NewWindow
  105.         (nil,&qd.screenBits.bounds,"\000",TRUE,plainDBox,(Ptr)-1,FALSE,0L);
  106.     SetPort(theWindow);
  107.     DisposeWindow(theWindow);
  108.  
  109.     SetUpMenus();
  110.     InitDialogs(DieGraceFully);    
  111.  
  112.     OpenPort(&srcport);
  113.     srcport.portBits.rowBytes = 32;
  114.     SetRect(&srcport.portBits.bounds,0,0,256,256);
  115.     srcport.portBits.baseAddr = NewPtr(256L*256L/8L);
  116.     if(srcport.portBits.baseAddr == nil){
  117.         SysError(dsMemFullErr);
  118.     }
  119.     SetPort(&srcport);
  120.     EraseRect(&srcport.portBits.bounds);
  121.     MoveTo(0,0);
  122.     PenSize(24,24);
  123.     LineTo(srcport.portBits.bounds.right,srcport.portBits.bounds.bottom);
  124.     MoveTo(srcport.portBits.bounds.right-12,srcport.portBits.bounds.top-12);
  125.     LineTo(srcport.portBits.bounds.left-12,srcport.portBits.bounds.bottom-12);
  126.     PenSize(1,1);
  127.  
  128.     theWindow = GetNewWindow( 256, &wRecord, (Ptr)-1L );
  129.     
  130.  
  131.     SizeWindow(theWindow,256+15,256+15,TRUE);
  132.     ShowWindow(theWindow);
  133.     SetPort(theWindow);
  134.     theWindow->txFont = 2;
  135.  
  136.     ResizePRect();
  137.  
  138.     vScroll = GetNewControl( 256, theWindow);
  139.     hScroll = GetNewControl( 257, theWindow);
  140.     MoveScrollBars();
  141.     theOrigin.h = 0; 
  142.     theOrigin.v = 0;
  143.  
  144.     do {    /* WELCOME TO THE MAIN EVENT LOOP. */
  145.         SystemTask();
  146.         if(XCursor != nil){
  147.             SetCursor(*XCursor);
  148.         }
  149.         else{
  150.             SetCursor(&qd.arrow);
  151.         }
  152.         GetNextEvent(everyEvent,&myEvent);
  153.         switch(myEvent.what) {
  154.             case mouseDown : 
  155.                 code = FindWindow(myEvent.where, &whichWindow);
  156.                 switch(code) {
  157.                     case inMenuBar :
  158.                         DoCommand(MenuSelect(myEvent.where));
  159.                         break;    
  160.                     case inSysWindow :
  161.                         SystemClick(&myEvent,whichWindow);
  162.                         break;
  163.                     case inDrag :
  164.                         DragWindow(whichWindow,myEvent.where,
  165.                                     &dragRect);
  166.                         break;
  167.                     case inGoAway :
  168.                         if (TrackGoAway(whichWindow,myEvent.where))
  169.                             doneFlag = TRUE;
  170.                         break;
  171.                     case inGrow :
  172.                         if (whichWindow == FrontWindow())
  173.                             GrowWnd(whichWindow);
  174.                         else
  175.                             SelectWindow(whichWindow);
  176.                         break;
  177.                     case inContent :
  178.                         if (whichWindow != FrontWindow())
  179.                             SelectWindow(whichWindow);
  180.                         else {
  181.                             GlobalToLocal(&myEvent.where);
  182.                             if (!(PtInRect(myEvent.where,&pRect))){
  183.                                 MyControl = FindControl(myEvent.
  184.                                                         where,
  185.                                                         whichWindow,
  186.                                                         &whichControl);
  187.                                 switch(MyControl) {
  188.                                     case inUpButton :
  189.                                         TrackControl(whichControl,
  190.                                                     myEvent.where,
  191.                                                     ScrollUp);
  192.                                         break;
  193.                                     case inDownButton :
  194.                                         TrackControl(whichControl,
  195.                                                     myEvent.where,
  196.                                                     ScrollDown);
  197.                                         break;
  198.                                     case inPageUp :
  199.                                         PageScroll(MyControl,-10);
  200.                                         break;
  201.                                     case inPageDown :
  202.                                         PageScroll(MyControl,10);
  203.                                         break;
  204.                                     case inThumb :
  205.                                         t = TrackControl(whichControl,
  206.                                                     myEvent.where,
  207.                                                     0L);
  208.                                         ScrollBits();
  209.                                         break;
  210.                                 }
  211.                             }
  212.                         }
  213.                         break; /* in content */
  214.                     }
  215.                 break;
  216.             case activateEvt:
  217.                 if (theWindow != (WindowPtr)myEvent.message)
  218.                     break;
  219.                 DrawGrowIcon(theWindow);
  220.                 if (myEvent.modifiers&1) {
  221.                     SetPort(theWindow);    
  222.                     ShowControl(vScroll);
  223.                     ShowControl(hScroll);
  224.                 }
  225.                 else {
  226.                     HideControl(vScroll);
  227.                     HideControl(hScroll);
  228.                 }
  229.                 break;
  230.             case updateEvt :
  231.                 GetPort(&savePort);
  232.                 SetPort(theWindow);
  233.                 BeginUpdate(theWindow);
  234.                 EraseRect(&theWindow->portRect);
  235.                 DrawWindow(theWindow);
  236.                 EndUpdate(theWindow);
  237.                 SetPort(savePort);
  238.                 break;
  239.             }
  240.         }
  241.         while ( !doneFlag ) ;
  242.         exit (0);
  243. }
  244. /*
  245.  * An offscreen BitMap is used to hold the image last loaded into the
  246.  * program.  On an update event, we CopyBits into the onscreen
  247.  * window.
  248.  */
  249. DrawStuff()
  250. {
  251.     BackPat(&qd.white);
  252.     EraseRect(&srcport.portBits.bounds);
  253.     CopyBits(&srcport.portBits,&theWindow->portBits,&srcport.portBits.bounds,
  254.         &srcport.portBits.bounds,srcCopy,nil);
  255. }
  256. /*
  257.  * This section deals with input and output of 'PICT' files.
  258.  */
  259. long PICTCount;
  260. short globalRef;
  261. PicHandle PICTHand;
  262. static Point SFWhere = { 0x0050, 0x0050 };
  263.  
  264. pascal void PutPICTData(dataPtr,byteCount)
  265. Ptr dataPtr;
  266. short byteCount;
  267. {
  268.     long longCount;
  269.     longCount = (long)byteCount;
  270.     PICTCount += byteCount;
  271.     (void)FSWrite(globalRef,&longCount,dataPtr);
  272.     if(PICTHand != nil){
  273.         (**PICTHand).picSize = PICTCount;
  274.     }
  275. }
  276.  
  277. SpoolOutPICTFile()
  278. {
  279.     OSErr err;
  280.     short i;
  281.     long longCount,longZero;
  282.     Rect pFrame;
  283.     SFReply reply;
  284.     QDProcs myprocs;
  285.     GrafPtr tmpport;
  286.     SFPutFile(SFWhere,"\PSave as PICT file:","\PUntitled",nil,&reply);
  287.     if(reply.good){
  288.         GetPort(&tmpport);
  289.         SetPort(theWindow);
  290.         err = FSDelete(&reply.fName,reply.vRefNum);
  291.         err = Create(&reply.fName,reply.vRefNum,'????','PICT');
  292.         if(err != noErr){
  293.             return;
  294.         }
  295.         (void)FSOpen(&reply.fName,reply.vRefNum,&globalRef);
  296.         SetStdProcs(&myprocs);
  297.         theWindow->grafProcs = &myprocs;
  298.         myprocs.putPicProc = (Ptr)PutPICTData;
  299.         longZero = 0;
  300.         longCount = 4;
  301.         PICTCount = sizeof(Picture);
  302.         for(i=0;i++< (512/4 + sizeof(Picture));){
  303.             longCount = 4;
  304.             (void)FSWrite(globalRef,&longCount,&longZero);
  305.         }
  306.         (void)SetFPos(globalRef,fsFromStart,522L);
  307.         pFrame = srcport.portBits.bounds;
  308.         PICTHand = nil;
  309.         ClipRect(&pFrame);
  310.         PICTHand = OpenPicture(&pFrame);
  311.         DrawStuff();
  312.         ClosePicture();
  313.         (void)SetFPos(globalRef,fsFromStart,512L);
  314.         longCount = 10L;
  315.         (void)FSWrite(globalRef,&longCount,*PICTHand);
  316.         FSClose(globalRef);
  317.         theWindow->grafProcs = nil;
  318.         KillPicture(PICTHand);
  319.         SetPort(tmpport);
  320.     }
  321. }
  322. pascal void GetPICTData(dataPtr,byteCount)
  323. Ptr dataPtr;
  324. short    byteCount;
  325. {
  326.     long longCount;
  327.     short err;
  328.     longCount = (long)byteCount;
  329.     err = FSRead(globalRef,&longCount,dataPtr);
  330. }
  331. GetandDrawPICTFile()
  332. {
  333.     SFReply reply;
  334.     GrafPtr tmpport;
  335.     QDProcs myprocs;
  336.     PicHandle PICTHand;
  337.     long longCount;
  338.     Rect pFrame;
  339.     static SFTypeList list = {'PICT'};
  340.     SFGetFile(SFWhere,0L,nil,1,list,0L,&reply);
  341.     if(reply.good && (FSOpen(&reply.fName,reply.vRefNum,&globalRef) == noErr)){
  342.         GetPort(&tmpport);
  343.         SetPort(theWindow);
  344.         SetWTitle(theWindow,&reply.fName);
  345.         InvalRect(&theWindow->portRect);
  346.         SetPort(&srcport);
  347.         SetStdProcs(&myprocs);
  348.         srcport.grafProcs = &myprocs;
  349.         myprocs.getPicProc = (Ptr)GetPICTData;
  350.         PICTHand = (PicHandle)NewHandle((long)sizeof(Picture));
  351.         SetFPos(globalRef,fsFromStart,512L);
  352.         longCount = 10L;
  353.         HLock(PICTHand);
  354.         (void)FSRead(globalRef,&longCount,*PICTHand);
  355.         srcport.portBits.bounds = pFrame = (**PICTHand).picFrame;
  356.         srcport.portRect = srcport.portBits.bounds;
  357.         if(srcport.portBits.baseAddr != nil){
  358.             DisposPtr(srcport.portBits.baseAddr);
  359.         }
  360.         srcport.portBits.baseAddr = NewPtr((long)((long)(pFrame.right-pFrame.left)*
  361.          (long)(pFrame.bottom-pFrame.top))/8L);
  362.         if(srcport.portBits.baseAddr == nil){
  363.             (void)FSClose(globalRef);
  364.             SysError(dsMemFullErr);
  365.         }
  366.         srcport.portBits.rowBytes = (pFrame.right-pFrame.left)/8;
  367.         ClipRect(&pFrame);
  368.         RectRgn(srcport.visRgn,&pFrame);
  369.         EraseRect(&pFrame);
  370.         HUnlock(PICTHand);
  371.         DrawPicture(PICTHand,&pFrame);
  372.         (void)FSClose(globalRef);
  373.         DisposHandle(PICTHand);
  374.         srcport.grafProcs = nil;
  375.         SizeWindowToImage();
  376.     }
  377. }
  378. SizeWindowToImage()
  379. {
  380.     Rect pFrame;
  381.     pFrame = srcport.portBits.bounds;
  382.     
  383.     SetPort(theWindow);
  384.     OffsetRect(&pFrame,-pFrame.left,-pFrame.top);
  385.     SetOrigin(0,0);
  386.     SetPt(&theOrigin,0,0);
  387.     SetCtlValue(vScroll,0);
  388.     SetCtlValue(hScroll,0);
  389.     pFrame.right += 15;
  390.     pFrame.bottom += 15;
  391.     LocalToGlobal(&pFrame.top);
  392.     LocalToGlobal(&pFrame.bottom);
  393.     SectRect(&pFrame,&dragRect,&pFrame);
  394.  
  395.     SizeWindow(theWindow,pFrame.right-pFrame.left,pFrame.bottom-pFrame.top,TRUE);
  396.     MoveScrollBars();
  397.     ResizePRect();
  398.  
  399. }
  400. /* 
  401.  * This section deals with the input and output of X11 Bitmap files.
  402.  * For a change, stdio is used.
  403.  */
  404. /*
  405.  * Bit and byte order is reversed between QuickDraw BitMaps and
  406.  * X11 Bitmaps.
  407.  */
  408. char hextab[256];
  409. void inithextab()
  410. {
  411. static int hextabinited = 0;
  412.     if (hextabinited == 0){
  413.         hextabinited = 1;
  414.         
  415.         hextab[0] = '0';    hextab[1] = '8';
  416.         hextab[2] = '4';    hextab[3] = 'c';
  417.         hextab[4] = '2';    hextab[5] = 'a';
  418.         hextab[6] = '6';    hextab[7] = 'e';
  419.         hextab[8] = '1';    hextab[9] = '9';
  420.         hextab[10] = '5';    hextab[11] = 'd';
  421.         hextab[12] = '3';    hextab[13] = 'b';
  422.         hextab[14] = '7';    hextab[15] = 'f';
  423.     
  424.         hextab['0'] = 0;    hextab['1'] = 8;
  425.         hextab['2'] = 4;    hextab['3'] = 12;
  426.         hextab['4'] = 2;    hextab['5'] = 10;
  427.         hextab['6'] = 6;    hextab['7'] = 14;
  428.         hextab['8'] = 1;    hextab['9'] = 9;
  429.         hextab['A'] = 5;    hextab['B'] = 13;
  430.         hextab['C'] = 3;    hextab['D'] = 11;
  431.         hextab['E'] = 7;    hextab['F'] = 15;
  432.         hextab['a'] = 5;    hextab['b'] = 13;
  433.         hextab['c'] = 3;    hextab['d'] = 11;
  434.         hextab['e'] = 7;    hextab['f'] = 15;
  435.     }
  436. }
  437. getXBitmap()
  438. {
  439.     SFReply frommac;
  440.     GrafPtr tmpport;
  441.     int width,height;
  442.     int c1,c2;
  443.     long nbytes;
  444.     char *ptr;
  445.     char ident[256];
  446.     static SFTypeList list = {'TEXT'};
  447.     SFGetFile(SFWhere,0L,nil,1,list,0L,&frommac);
  448.     if(frommac.good){
  449.         inithextab();
  450.         p2cstr(&frommac.fName);
  451.         SetVol(nil,frommac.vRefNum);
  452.         freopen(&frommac.fName,"r",stdin);
  453.         if(scanf("#define%s%d",ident,&width)!=2)return;
  454.         if(scanf(" #define%s%d",ident,&height)!=2)return;
  455.         ptr = ident;
  456.         while(*ptr){
  457.             if(ptr[0]=='_'){
  458.                 ptr[0] = '\0';
  459.                 break;
  460.             }
  461.             ptr++;
  462.         }
  463.         nbytes = ((long)width * (long)height)/8L;
  464.         if(srcport.portBits.baseAddr != nil){
  465.             DisposPtr(srcport.portBits.baseAddr);
  466.         }
  467.         ptr = srcport.portBits.baseAddr = NewPtr(nbytes);
  468.         if(ptr == nil){
  469.             SysError(dsMemFullErr);
  470.         }
  471.         srcport.portBits.rowBytes = width/8;
  472.         srcport.portBits.bounds.top = srcport.portBits.bounds.left = 0;
  473.         srcport.portBits.bounds.right = width;
  474.         srcport.portBits.bounds.bottom = height;
  475.         srcport.portRect = srcport.portBits.bounds;
  476.         
  477.         while((c1=getchar())!=EOF){
  478.             if(c1 == 'x'){
  479.                 c1 = getchar();
  480.                 c2 = getchar();
  481.                 *ptr++ = hextab[c1]+hextab[c2]*16;
  482.             }
  483.         }
  484.         GetPort(&tmpport);
  485.         SetPort(theWindow);
  486.         c2pstr(ident);
  487.         SetWTitle(theWindow,ident);
  488.         InvalRect(&theWindow->portRect);
  489.         SizeWindowToImage();
  490.         SetPort(tmpport);
  491.     }else{
  492.         SetRect(&srcport.portBits.bounds,0,0,200,200);
  493.     }
  494. putXBitmap()
  495. {
  496.     SFReply reply;
  497.     static SFTypeList list = {'TEXT'};
  498.     unsigned char *ptr;
  499.     long bytes,i;
  500.     FILE *fd;
  501.     SFPutFile(SFWhere,"\PSave as TEXT file:","\PUntitled",nil,&reply);
  502.     if(reply.good){
  503.         inithextab();
  504.         Create(&reply.fName,reply.vRefNum,'EDIT','TEXT');
  505.         p2cstr(&reply.fName);
  506.         SetVol(nil,reply.vRefNum);
  507.         fd = fopen(&reply.fName,"w");
  508.         ptr = (unsigned char *)&reply.fName;
  509.         while(*ptr){
  510.             if(*ptr == '.'){
  511.                 *ptr = '\0';
  512.                 break;
  513.             }
  514.             ptr++;
  515.         }
  516.         if(fd == NULL)return;
  517.         fprintf(fd,
  518.          "#define %s_width %d\n#define %s_height %d\nstatic char %s_bits[] = {\n ",
  519.           &reply.fName,
  520.            srcport.portBits.bounds.right - srcport.portBits.bounds.left,
  521.             &reply.fName,
  522.              srcport.portBits.bounds.bottom - srcport.portBits.bounds.top,
  523.               &reply.fName);
  524.         ptr = (unsigned char*)srcport.portBits.baseAddr;
  525.         bytes = GetPtrSize(ptr);
  526.         for(i=0L;i++<bytes;){
  527.             fprintf(fd,"0x%c%c",hextab[ptr[0]&0x0F],hextab[(ptr[0]>>4)&0x0F]);
  528.             if(i<bytes){
  529.                 fprintf(fd,",");
  530.             }
  531.             if(i%15L == 0L){
  532.                 fprintf(fd,"\n ");
  533.             }
  534.             ptr++;
  535.         }
  536.         fprintf(fd,"};\n");
  537.         fclose(fd);
  538.     }
  539. }
  540.  
  541. MoveScrollBars()
  542. {
  543.     HideControl(vScroll);
  544.     MoveControl(vScroll,theWindow->portRect.right-15,theWindow->
  545.                                 portRect.top-1);
  546.     SizeControl(vScroll,16,theWindow->portRect.bottom-theWindow->
  547.                                 portRect.top-13);
  548.     ShowControl(vScroll);
  549.     HideControl(hScroll);
  550.     MoveControl(hScroll,theWindow->portRect.left-1,theWindow->
  551.                                 portRect.bottom-15);
  552.     SizeControl(hScroll,theWindow->portRect.right-theWindow->
  553.                                 portRect.left-13,16);
  554.     ShowControl(hScroll);
  555. }
  556.  
  557. ResizePRect()
  558. {
  559.     pRect = theWindow->portRect;
  560.     pRect.right = pRect.right - 15;
  561.     pRect.bottom = pRect.bottom - 15;
  562. }
  563.  
  564. GrowWnd(whichWindow)
  565. WindowPtr whichWindow;
  566. {
  567.     long longResult;
  568.     short height, width;
  569.     Rect tRect;
  570.     
  571.     tRect = srcport.portBits.bounds;
  572.     if(tRect.right == 0)tRect.right = 84;
  573.     if(tRect.bottom == 0)tRect.bottom = 84;
  574.     OffsetRect(&tRect,16,16);
  575.  
  576.     longResult = GrowWindow(whichWindow,myEvent.where,&tRect);
  577.  
  578.     if ( longResult == 0 )
  579.         return;
  580.  
  581.     height = longResult >> 16;
  582.     width = longResult;
  583.  
  584.     /* add the old "scroll bar area" to the update region so it will */
  585.     /* be redrawn (for when the window is enlarged) */
  586.     tRect = whichWindow->portRect;
  587.     tRect.left = tRect.right - 16;
  588.     InvalRect(&tRect);
  589.     tRect = whichWindow->portRect;
  590.     tRect.top = tRect.bottom - 16;
  591.     InvalRect(&tRect);
  592.  
  593.     /* now draw the newly sized window */
  594.     SizeWindow(whichWindow,width,height,TRUE);
  595.     MoveScrollBars();
  596.     ResizePRect();
  597.  
  598.     /* add the new "scroll bar area" to the update region so it will 
  599.        be redrawn (for when the window is made smaller) */
  600.     tRect = whichWindow->portRect;
  601.     tRect.left = tRect.right - 16;
  602.     InvalRect(&tRect);
  603.     tRect = whichWindow->portRect;
  604.     tRect.top = tRect.bottom - 16;
  605.     InvalRect(&tRect);
  606.     ScrollBits();
  607. }
  608.  
  609. DrawWindow(whichWindow)
  610. WindowPtr whichWindow;
  611. {
  612.     Rect tRect;
  613.  
  614.     ClipRect(&(theWindow->portRect));
  615.     DrawGrowIcon(theWindow);
  616.  
  617.         DrawControls(theWindow);
  618.  
  619.         /* now set up a clip area which excludes the scroll bars */
  620.  
  621.         tRect = theWindow->portRect;
  622.         tRect.bottom = tRect.bottom - 15;
  623.         tRect.right = tRect.right - 15;    /* Was 16 */
  624.  
  625.         /* now compensate for any scrolling which has been done */
  626.  
  627.         OffsetRect(&tRect, theOrigin.h, theOrigin.v);
  628.         ClipRect(&tRect);
  629.  
  630.         /* change the origin to compensate for any scrolling */
  631.  
  632.         SetOrigin(theOrigin.h, theOrigin.v);
  633.         DrawStuff();
  634.         SetOrigin(0,0);
  635.  
  636.         ClipRect(&(theWindow->portRect)); /* reset the clip area */
  637. }
  638.  
  639. ScrollBits()
  640. {
  641.     Point oldOrigin;
  642.     int dh, dv, t1, t2;
  643.     Rect tRect;
  644.  
  645.     oldOrigin = theOrigin;
  646.     t1 = (srcport.portBits.bounds.right - pRect.right);
  647.     t2 = (srcport.portBits.bounds.bottom - pRect.bottom);
  648. /*
  649.  * The next step has to be done in long arithmetic, because a short int
  650.  * can overflow if the srcport portRect is large enough.
  651.  */
  652.     theOrigin.h = ((long)t1 * (long)GetCtlValue(hScroll))/50L;
  653.     theOrigin.v = ((long)t2 * (long)GetCtlValue(vScroll))/50L;
  654.     dh = oldOrigin.h - theOrigin.h;
  655.     dv = oldOrigin.v - theOrigin.v;
  656.     theUpdateRgn = NewRgn();
  657.     ScrollRect( &pRect, dh, dv, theUpdateRgn);
  658.  
  659.     /* have scrolled in junk... need to redraw */
  660.  
  661.     SetOrigin(theOrigin.h, theOrigin.v);
  662.     OffsetRect(&(*theUpdateRgn)->rgnBBox,theOrigin.h, theOrigin.v);
  663.     ClipRect(&(*theUpdateRgn)->rgnBBox);
  664.     DrawStuff();
  665.     DisposeRgn(theUpdateRgn);
  666.     SetOrigin(0,0);
  667.     ClipRect(&(theWindow->portRect));
  668. }
  669.  
  670. pascal void ScrollUp(whichControl,theCode)
  671. ControlHandle whichControl;
  672. short theCode;
  673. {
  674.     if ( theCode == inUpButton ) {
  675.         SetCtlValue(whichControl,GetCtlValue(whichControl)-1);
  676.         ScrollBits();
  677.     }
  678. }
  679.  
  680. pascal void ScrollDown(whichControl,theCode)
  681. ControlHandle whichControl;
  682. short theCode;
  683. {
  684.     if ( theCode == inDownButton ) {
  685.         SetCtlValue(whichControl,GetCtlValue(whichControl)+1);
  686.         ScrollBits();
  687.     }
  688. }
  689.  
  690. PageScroll(code,amount)
  691. int code, amount;
  692. {
  693.     Point myPt;
  694.  
  695.     do {    
  696.         GetMouse(&myPt);
  697.         if ( TestControl(whichControl,myPt) == code ) {
  698.             SetCtlValue(whichControl,GetCtlValue(whichControl)+amount);
  699.             ScrollBits();
  700.         }
  701.     }
  702.     while (StillDown());
  703. }
  704.  
  705. SetUpMenus()
  706. {
  707.     int i;
  708.  
  709.     InitMenus();
  710.     myMenu[0] = GetMenu(appleMenu);
  711.     AddResMenu(myMenu[0],'DRVR'); /* desk accesories */
  712.     myMenu[1] = GetMenu(fileMenu);
  713.     myMenu[2] = GetMenu(editMenu);
  714.  
  715.     for ( i = 0; i < lastMenu; i++ )
  716.         InsertMenu(myMenu[i],0);
  717.         
  718.     DrawMenuBar();
  719. }
  720.  
  721. DoCommand(mResult)
  722. unsigned long mResult;
  723. {
  724.     char name[30];
  725.     short theMenu, theItem;
  726.  
  727.     theMenu = mResult >> 16;
  728.     theItem = mResult;
  729.  
  730.     switch (theMenu) {
  731.         case appleMenu :
  732.             if(theItem == 1){
  733.                 Alert(128,nil);
  734.             }else{
  735.                 GetItem(myMenu[0],theItem,name);
  736.                 OpenDeskAcc(name);
  737.             }
  738.             break;
  739.  
  740.         case fileMenu :
  741.             switch(theItem){
  742.                 case openX:
  743.                     getXBitmap();
  744.                     break;
  745.                 case saveX:
  746.                     putXBitmap();
  747.                     break;
  748.                 case openPICT:
  749.                     GetandDrawPICTFile();
  750.                     break;
  751.                 case savePICT:
  752.                     SpoolOutPICTFile();
  753.                     break;
  754.                 case Quit:
  755.                     doneFlag = TRUE;
  756.                     break;
  757.                 default:
  758.                     break;
  759.             }
  760.         case editMenu:
  761.             (void)SystemEdit(theItem-1);
  762.             break;
  763.     }
  764.     HiliteMenu(0);
  765. }
  766.